package com.codeoss.framework.view.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.IScriptService;
import com.codeoss.framework.expression.IScriptServiceStore;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.service.IComponentService;

@Service
public class ComponentService implements IComponentService {
	@Autowired
	private IScriptServiceStore ScriptServiceStore;

	@Override
	public void processEvent(YunContext yunContext, Component component, String eventType) {
		// TODO Auto-generated method stub
		if ("clickEvent".equals(eventType)) {
			Expression onClickExpression = component.getOnClickExpression();
			if (onClickExpression != null) {
				executeEventExpression(yunContext, component, onClickExpression,null);
			}
		}
	}
	
	protected void executeEventExpression(YunContext yunContext,Component component,Expression eventExpression,Map<String,Object> paraMap){
		Map<String, Object> bindingMap = new HashMap<String, Object>();
		if(paraMap!=null) {
			bindingMap.putAll(paraMap);
		}
		bindingMap.putAll(yunContext.getVarValueMap());
		IScriptService scriptService=ScriptServiceStore.getScriptService(eventExpression);
		scriptService.eval(eventExpression.getScript(), bindingMap);
	}

	@Override
	public void processValue(YunContext yunContext, Component component,Map<String,String> paraMap,Map<String,Object> dataMap) {
		// TODO Auto-generated method stub
		
	}
}
